package com.inossem.warehousestorage_inossem.adapter.storage;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.storage.StorageManageExtra;
import com.inossem.warehousestorage_inossem.activity.storage.StorageManageMatrMoveActivity;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMatrBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.storage.StorageManageMatrRequest;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.inossem.warehousestorage_inossem.utils.StorageManageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * Created by Administrator on 2017/7/16 0016.
 */

public class StorageManageMatrAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private EditText input;
    private String inputStr;
    private ImageView search;
    private List<StorageManageMatrBean.DataBean.MaterielListBean> list;

    public StorageManageMatrAdapter(Context mContext, List<StorageManageMatrBean.DataBean.MaterielListBean> list) {
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i - 1);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_storage_manage_list_first_item, null);
            input = (EditText) convertView.findViewById(R.id.input);
            search = (ImageView) convertView.findViewById(R.id.search);
            if (!TextUtils.isEmpty(inputStr)) {
                input.setText(inputStr);
            }
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    String no = input.getText().toString();
                    doSearch(no);

                }
            });

        } else {
            StorageManageMatrAdapter.ViewHolder viewHolder;
            if (convertView != null && convertView.getTag() != null) {
                //复用控件
                viewHolder = (StorageManageMatrAdapter.ViewHolder) convertView.getTag();
            } else {
                //构造列表项控件，用ItemView进行缓存
                convertView = inflater.inflate(R.layout.view_storage_manage_list_item, null);
                viewHolder = new StorageManageMatrAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            }
            //初始化数据
            StorageManageMatrBean.DataBean.MaterielListBean item = (StorageManageMatrBean.DataBean.MaterielListBean) getItem(position);
            initData(mContext, viewHolder, convertView, item);
        }

        return convertView;
    }

    public void doSearch(String no) {
        inputStr = no;
        StorageManageMatrRequest.request(mContext, no, new GsonBeanListener<StorageManageMatrBean>() {
            @Override
            public void onSuccess(StorageManageMatrBean bean) {
                //进行去重操作
                int oldCnt = StorageManageMatrAdapter.this.list.size();
                Utils.addNoDuplicate(bean.getData().getMaterielList(), StorageManageMatrAdapter.this.list);

                bean.getData().setMaterielList(StorageManageMatrAdapter.this.list);
                if (StorageManageMatrAdapter.this.list.size() > oldCnt) {


                    StorageManageMatrAdapter.this.notifyDataSetChanged();
                    SPUtils.saveInformation(mContext, SPUtils.KEY_STORAGE_MGR_MATR_INFORMATION, bean);

                }
            }
        });
    }

    private void initView(StorageManageMatrAdapter.ViewHolder viewHolder, View view) {
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.specialInventoryCode = (TextView) view.findViewById(R.id.specialInventoryCode);
        viewHolder.materielDescription = (TextView) view.findViewById(R.id.materielDescription);
        viewHolder.sendPosition = (TextView) view.findViewById(R.id.sendPosition);
        viewHolder.batchCode = (TextView) view.findViewById(R.id.batchCode);
        viewHolder.inputText = (TextView) view.findViewById(R.id.inputText);
        viewHolder.specialInventoryCodeTextView = (TextView) view.findViewById(R.id.specialInventoryCodeTextView);
        viewHolder.firstRow = (PercentRelativeLayout) view.findViewById(R.id.firstRow);


    }

    private void initData(Context context, StorageManageMatrAdapter.ViewHolder viewHolder, View view, StorageManageMatrBean.DataBean.MaterielListBean item) {
        viewHolder.materielCode.setText(item.getMaterielCode());
        viewHolder.specialInventoryCode.setText(item.getSpecialInventoryCode());
        viewHolder.materielDescription.setText(item.getMaterielDescription());
        viewHolder.sendPosition.setText(item.getSendPosition());
        viewHolder.batchCode.setText(item.getBatchCode());

        // 特殊库存编码为空则隐藏
        if (TextUtils.isEmpty(item.getSpecialInventoryCode())) {
            viewHolder.specialInventoryCode.setVisibility(View.INVISIBLE);
            viewHolder.specialInventoryCodeTextView.setVisibility(View.INVISIBLE);
        }

        if (item.getUpList().size() > 0) {
            viewHolder.inputText.setText(StorageManageUtils.getUpCount(item) + item.getUnit());
            viewHolder.firstRow.setBackgroundColor(ContextCompat.getColor(context, R.color.base_green));
        } else {
            //因为有可能会复用之前的渲染，需要根据数据重新设置组件的可见性
            viewHolder.inputText.setText(context.getString(R.string.view_storage_manage_list_item_inputText));
            viewHolder.firstRow.setBackgroundColor(ContextCompat.getColor(context, R.color.base_gray));
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                goMoveAct(item);
            }
        });

    }

    private void goMoveAct(StorageManageMatrBean.DataBean.MaterielListBean item) {
        Intent intent = new Intent(mContext, StorageManageMatrMoveActivity.class);
        intent.putExtra(StorageManageExtra.ID, item.getId());
        mContext.startActivity(intent);
    }

    private class ViewHolder {
        private TextView
                materielCode,
                specialInventoryCodeTextView,
                specialInventoryCode,
                materielDescription,
                sendPosition,
                batchCode, inputText;
        private PercentRelativeLayout firstRow;
    }

    public void setList(List<StorageManageMatrBean.DataBean.MaterielListBean> list) {
        this.list = list;
    }
}
